// pages/testList/testList.js
const request = require('../../request/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: '1',
    base: [{
        name: "gender",
        label: "性别",
        tab: ['男', '女']
      },
      {
        name: "ever_married",
        label: "是否结婚",
        value: "",
        tab: ['否', '是']
      },
      {
        name: "Residence_type",
        label: "居住类型",
        value: "",
        tab: ['农村', '城市']
      },
      {
        name: "hypertension",
        label: "是否患有高血压",
        value: "",
        tab: ['否', '是']
      },
      {
        name: "heart_disease",
        label: "是否患有心脏病",
        value: "",
        tab: ['否', '是']
      }
    ],
    data1: [{
        name: "gender",
        value: null
      },
      {
        name: "ever_married",
        value: null
      },
      {
        name: "Residence_type",
        value: null
      },
      {
        name: "hypertension",
        value: null
      },
      {
        name: "heart_disease",
        value: null
      },
      {
        name: "work_type",
        value: null
      },
      {
        name: "smoking_status",
        value: null
      },
      {
        name: "age",
        value: null
      },
      {
        name: "height",
        value: null
      },
      {
        name: "weight",
        value: null
      },
      {
        name: "avg_glucose_level",
        value: null
      }
    ],
    //储存所有提交的数据

  },
  // 单选框
  onChangeradio(e) {
    console.log(e)
    let value = e.target.dataset
    let data1 = this.data.data1
    for (let i = 0; i < data1.length; i++) {
      if (data1[i].name == value.name) {
        data1[i].value = Number(value.value)
        console.log(data1)
      }
    }
    this.setData({
      data1
    })


  },
  // 输入框
  onChangeinput(e) {
    console.log(e)
    let name = e.target.dataset.name
    let value = e.detail.value
    let data1 = this.data.data1
    for (let i = 0; i < data1.length; i++) {
      if (data1[i].name == name) {
        data1[i].value = Number(value)
        console.log(data1)
      }
    }
    this.setData({
      data1
    })
  },
  // 提交
  sumbitForm(e) {
    console.log(e)
    let data1 = this.data.data1
    // 遍历是否全部都输入了
    for (let i = 0; i < data1.length; i++) {
      console.log(data1[i].value)
      if (data1[i].value == null) {
        console.log(1)
        wx.showModal({
          content: '请输入所有数据',
          showCancel: false,
          success: (e) => {
            if (e.confirm == true) {
              console.log('用户点击确定')
            }
          }
        })
        return

      }
    }
    // 提交表单
    console.log(data1)
    let weight = data1[9].value
    let height = data1[8].value
    let fromdata = {
      gender: data1[0].value,
      ever_married: data1[1].value,
      Residence_type: data1[2].value,
      hypertension: data1[3].value,
      heart_disease: data1[4].value,
      work_type: data1[5].value,
      smoking_status: data1[6].value,
      age: data1[7].value,
      avg_glucose_level: data1[10].value,
      bmi: weight / (height * height),
      weight: weight,
      height: height,
      openid: this.data.userInfo.openid
    }
    console.log(fromdata)

    request.wxRequest("https://zf.asong.life/zf/predict", "POST", fromdata).then(async res => {
      console.log(res)
      // 设置缓存
      let newData = res
      wx.setStorageSync('newData', newData)
  
      // 转跳
      wx.navigateTo({
        url: './report/report',
      })
    }).catch(err => {
      console.log(err)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) {
      wx.showModal({
        content: '还未登录,请先登录',
        showCancel: false,
        complete: (res) => {
          if (res.confirm) {
            console.log(123)
            wx.switchTab({
              url: '../my/my',
            })
          }
        }
      })
    }
    this.setData({
      userInfo
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})